package com.fruitshop.util;

import com.fruitshop.platform.Validator;

import java.math.BigDecimal;
import java.util.InputMismatchException;
import java.util.Scanner;

public class ConsoleUtils {
    public static int readInt(Scanner scanner, String prompt, Validator<Integer> validator) {
        int result = 0;
        while (true) {
            try {
                PrintUtils.printHighLight(prompt);
                result = scanner.nextInt();
                scanner.nextLine();
                if(!validator.validate(result)) {
                    throw new Exception();
                }
                return result;
            }
            catch (InputMismatchException e) {
                scanner.next(); // 清除输入缓冲区中的错误数据
                PrintUtils.printError("输入不合法，请重新输入.");
                PrintUtils.newLine();
            }catch (Exception e){
                PrintUtils.printError("输入不合法，请重新输入.");
                PrintUtils.newLine();
            }
        }
    }
    public static int readIntDefault(Scanner scanner, String prompt,Integer defaultValue, Validator<Integer> validator) {
        int result = 0;
        while (true) {
            try {
                PrintUtils.printHighLight(prompt);
                String resultStr = scanner.nextLine();
                if(resultStr.isEmpty()) {
                    result = defaultValue;
                }
                else {
                    result = Integer.parseInt(resultStr);
                }
                if(!validator.validate(result)) {
                    throw new Exception();
                }
                return result;
            }
            catch (InputMismatchException e) {
                PrintUtils.printError("输入不合法，请重新输入.");
                PrintUtils.newLine();
            }catch (Exception e){
                PrintUtils.printError("输入不合法，请重新输入.");
                PrintUtils.newLine();
            }
        }
    }

    public static int readInt(Scanner scanner, String prompt) {
        int result = 0;
        while (true) {
            try {
                PrintUtils.printHighLight(prompt);
                result = scanner.nextInt();

                return result;
            }
            catch (InputMismatchException e) {
                scanner.next(); // 清除输入缓冲区中的错误数据
                PrintUtils.printError("输入不合法，请重新输入.");
                PrintUtils.newLine();
            }catch (Exception e){
                PrintUtils.printError("输入不合法，请重新输入.");
                PrintUtils.newLine();
            }
        }
    }

    public static int readIntDefault(Scanner scanner, String prompt,Integer defaultValue) {
        int result = 0;
        while (true) {
            try {
                PrintUtils.printHighLight(prompt);
                String resultStr = scanner.nextLine();
                if(StringUtils.isEmpty(resultStr)) {
                    result = defaultValue;
                } else {
                    result = Integer.parseInt(resultStr);
                }
                return result;
            }
            catch (InputMismatchException e) {
                PrintUtils.printError("输入不合法，请重新输入.");
                PrintUtils.newLine();
            }catch (Exception e){
                PrintUtils.printError("输入不合法，请重新输入.");
                PrintUtils.newLine();
            }
        }
    }

    public static String readString(Scanner scanner, String prompt) {
        PrintUtils.printHighLight(prompt);
       return scanner.next();
    }

    public static String readStringDefault(Scanner scanner, String prompt,String defaultValue) {
        PrintUtils.printHighLight(prompt);
        String result = scanner.nextLine();
        if(StringUtils.isEmpty(result)) {
            return defaultValue;
        }
        return result;
    }
    public static String prompt(Scanner scanner, String prompt) {
        PrintUtils.printHighLight(prompt);
        return scanner.nextLine();
    }



    public static BigDecimal readDecimal(Scanner scanner, String prompt, Validator<BigDecimal> validator) {
        BigDecimal result = null;
        while (true) {
            try {
                PrintUtils.printHighLight(prompt);
                String resultStr = scanner.next();
                result = new BigDecimal(resultStr);
                scanner.nextLine();
                if(!validator.validate(result)) {
                    throw new Exception();
                }
                return result;
            }
            catch (InputMismatchException e) {
                scanner.next(); // 清除输入缓冲区中的错误数据
                PrintUtils.printError("输入不合法，请重新输入.");
                PrintUtils.newLine();
            }catch (Exception e){
                PrintUtils.printError("输入不合法，请重新输入.");
                PrintUtils.newLine();
            }
        }
    }

    public static BigDecimal readDecimalDefault(Scanner scanner, String prompt,BigDecimal defaultValue, Validator<BigDecimal> validator) {
        BigDecimal result = null;
        while (true) {
            try {
                PrintUtils.printHighLight(prompt);
                String resultStr = scanner.nextLine();
                if(StringUtils.isEmpty(resultStr)) {
                    result = defaultValue;
                } else {
                    result = new BigDecimal(resultStr);
                }
                if(!validator.validate(result)) {
                    throw new Exception();
                }
                return result;
            }
            catch (InputMismatchException e) {
                PrintUtils.printError("输入不合法，请重新输入.");
                PrintUtils.newLine();
            }catch (Exception e){
                PrintUtils.printError("输入不合法，请重新输入.");
                PrintUtils.newLine();
            }
        }
    }
}
